/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "GuiRectangle.h"

#include "../Content/PixelShader.h"
#include "../Content/GeometryShader.h"
#include "../Content/VertexShader.h"

#include <tinyxml.h>

namespace Hogshead
{
	namespace DX11Rendering
	{
		ComponentCPP(Hogshead::DX11Rendering::GuiRectangle)
		GuiRectangle::GuiRectangle(void)
		{
			
			_gs = NULL;
			_ps = NULL;
			_vs = NULL;
		}


		GuiRectangle::~GuiRectangle(void)
		{
			
		}


		//Finalize Method
		//	-release the DX11GuiRectangleInfo object that we have acquired
		//	 from the Factory
		void GuiRectangle::finalize()
		{
			
			if(_gs)	ContentLibrary::release(_gs);
			if(_ps) ContentLibrary::release(_ps);
			if(_vs) ContentLibrary::release(_vs);
		}

		//Clone Method
		//	-Acquire a new GuiRectangle object
		//	-Copy all variables over to the new object
		IComponent* GuiRectangle::clone() const
		{
			GuiRectangle* new_gui_rect = Factory::acquire<GuiRectangle>();

			

			//Acquire new content objects for the new GuiRectangle object
			new_gui_rect->_vs = ContentLibrary::getContent<VertexShader>(_vs->getKey());
			new_gui_rect->_gs = ContentLibrary::getContent<GeometryShader>(_gs->getKey());
			new_gui_rect->_ps = ContentLibrary::getContent<PixelShader>(_ps->getKey());
			
			return new_gui_rect;
		}

		//readXML Method
		//	-Traverse through the xml node and it's children to get all
		//	 the information necessary to create a GuiRectangle object
		IComponent* GuiRectangle::readXML(const String& in_xml)
		{
			IComponent::readXML(in_xml);


			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			if(root_element)
			{
				if(String::equals(root_element->Value(), "Component"))
				{
					
					//Read in all the attributes of the GuiRectangle node
					float rect_upper_left_x, rect_upper_left_y, rect_extent_x,rect_extent_y,uv_upper_left_x,
						uv_upper_left_y, uv_extents_x, uv_extents_y;
					root_element->QueryFloatAttribute("rect_upper_left_x", &rect_upper_left_x); 
					root_element->QueryFloatAttribute("rect_upper_left_y", &rect_upper_left_y); 
					root_element->QueryFloatAttribute("rect_extent_x", &rect_extent_x); 
					root_element->QueryFloatAttribute("rect_extent_y", &rect_extent_y); 
					root_element->QueryFloatAttribute("uv_upper_left_x", &uv_upper_left_x); 
					root_element->QueryFloatAttribute("uv_upper_left_y", &uv_upper_left_y); 
					root_element->QueryFloatAttribute("uv_extents_x", &uv_extents_x); 
					root_element->QueryFloatAttribute("uv_extents_y", &uv_extents_y); 

					/*_vertex_info->_vertex._rect_upper_left.x = rect_upper_left_x;
					_vertex_info->_vertex._rect_upper_left.y = rect_upper_left_y;
					_vertex_info->_vertex._rect_extents.x = rect_extent_x;
					_vertex_info->_vertex._rect_extents.y = rect_extent_y;
					_vertex_info->_vertex._uv_upper_left.x = uv_upper_left_x;
					_vertex_info->_vertex._uv_upper_left.y = uv_upper_left_y;
					_vertex_info->_vertex._uv_extents.x = uv_extents_x;
					_vertex_info->_vertex._uv_extents.y = uv_extents_y;
*/

					//Iterate through all the children nodes of the GuiRectangle object
					//in order to get shaders and texture needed for this object
					TiXmlNode* child_node = NULL;
					TiXmlElement* child_element;
					while(child_node = root_element->IterateChildren(child_node))
					{
			
						child_element = child_node->ToElement();
						if(!child_element)
							continue;
						
						String tag(child_element->Value());
						if(tag.equals("ContentXML"))
						{

						
							IContent* content_pointer = ContentLibrary::loadAndParseContentXML(String(child_element->Attribute("path")));
							//Read in the Vertex Shader
							if(String(child_element->Attribute("type")).equals("VertexShader"))
							{
								_vs = (VertexShader*)content_pointer;
							}
							//Read in the geometry shader
							else if(String(child_element->Attribute("type")).equals("GeometryShader"))
							{
								_gs = (GeometryShader*)content_pointer;
							}
							//read in the pixel shader
							else if(String(child_element->Attribute("type")).equals("PixelShader"))
							{
								_ps = (PixelShader*)content_pointer;
							}
							////read in the texture
							//else if(String(child_element->Attribute("type")).equals("Texture"))
							//{
							//	_vertex_info->_texture = (Texture*)content_pointer;
							//}
							////read in the sampler state
							//else if(String(child_element->Attribute("type")).equals("SamplerState"))
							//{
							//	_vertex_info->_sampler_state = (SamplerState*)content_pointer;
							//}
							////read in the rasterizer state
							//else if(String(child_element->Attribute("type")).equals("RasterizerState"))
							//{
							//	_vertex_info->_rasterizer_state = (RasterizerState*)content_pointer;
							//}
							////read in the blend state
							//else if(String(child_element->Attribute("type")).equals("BlendState"))
							//{
							//	_vertex_info->_blend_state = (BlendState*)content_pointer;
							//}
						}

					}
				}
			}
			return this;
		}

		//Initialize Method
		//	-Register this component with the DX11Director
		void GuiRectangle::initialize()
		{
			this->getParentGameObject()->getScene()->getDirector("DX11Director")->registerComponent(this);
		}

		
	}
}